package indi.luckly.config.shiro;

import indi.luckly.config.shiro.salt.MyByteSource;
import indi.luckly.config.shiro.token.SecureToken;
import indi.luckly.entity.PermissionEntity;
import indi.luckly.entity.RoleEntity;
import indi.luckly.entity.UserEntity;
import indi.luckly.mapper.IPermissionMapper;
import indi.luckly.mapper.IRoleMapper;
import indi.luckly.mapper.IUserMapper;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;

import java.util.HashMap;
import java.util.List;
import java.util.Objects;

public class CustomRealm extends AuthorizingRealm {

//    @Override
//    public boolean supports(AuthenticationToken token) {
//        return token instanceof SecureToken;
//    }

    /**
     * 授权
     */
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
        String userName = (String) principals.getPrimaryPrincipal();
        //查询该用户角色
        IUserMapper userMapper = (IUserMapper) SpringBeanUtils.getBean("userMapper");
        IRoleMapper roleMapper = (IRoleMapper) SpringBeanUtils.getBean("roleMapper");
        IPermissionMapper permissionMapper = (IPermissionMapper) SpringBeanUtils.getBean("permissionMapper");
        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put("user_name", userName);
        List<UserEntity> userEntities = userMapper.selectByMap(hashMap);
        if (userEntities == null || userEntities.size() == 0) {
            return null;
        }

        List<RoleEntity> roles = roleMapper.findRoleByUserId(userEntities.get(0).getId());
        SimpleAuthorizationInfo authorizationInfo = new SimpleAuthorizationInfo();
        for (RoleEntity roleEntity : roles) {
            authorizationInfo.addRole(roleEntity.getRoleName());
            List<PermissionEntity> permissions = permissionMapper.findPermissionByRoleId(roleEntity.getId());
            permissions.forEach(permission -> {
                authorizationInfo.addStringPermission(permission.getPermissionName());
                System.out.println(permission.toString());
            });
            System.out.println(roleEntity.toString());
        }
        return authorizationInfo;

    }


    /**
     * 认证
     */
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
        String userName = (String) token.getPrincipal();
        IUserMapper userMapper = (IUserMapper) SpringBeanUtils.getBean("userMapper");
        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put("user_name", userName);
        List<UserEntity> userEntities = userMapper.selectByMap(hashMap);
        if (userEntities == null || userEntities.size() == 0 || !Objects.equals(userName, userEntities.get(0).getUserName())) {
            return null;
        } else {
            UserEntity userEntity = userEntities.get(0);
            return new SimpleAuthenticationInfo(userEntity.getUserName(), userEntity.getPassword(), new MyByteSource(userEntity.getSalt()), getName());
        }
//        SecureToken secureToken = (SecureToken) token;
//        IUserMapper userMapper = (IUserMapper) SpringBeanUtils.getBean("userMapper");
//        HashMap<String, Object> hashMap = new HashMap<>();
//        hashMap.put("user_name", secureToken.getPrincipal());
//        //获取用户的输入的账号.
//        String sid = (String) usToken.getCredentials();
//
//        log.info("sid: " + sid);
//        return new SimpleAccount(sid, sid, "userRealm");
    }

    @Override
    public boolean supports(AuthenticationToken token) {
        return token instanceof SecureToken;
    }

    @Override
    protected Object getAuthorizationCacheKey(PrincipalCollection principals) {
        return super.getAuthorizationCacheKey(principals);
//        MallUserInfo userInfo = (MallUserInfo) principals.getPrimaryPrincipal();
//        return userInfo.getUserName();
    }

}
